package com.um.swing.ui;

import cn.hutool.core.util.ObjectUtil;
import com.um.swing.common.inc.CreateIcon;
import com.um.swing.common.inc.Constant;
import sun.swing.SwingUtilities2;

import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * IButton UI for PView
 *
 * @author chenxin <chenxin619315@gmail.com>
 */
public class IButtonUI extends BasicButtonUI {


    public static enum B_ICON {
        NEXT, PREVIOUS,
        ROTATE_LEFT, ROTATE_RIGHT,
        FIT_BEST, ZOOM_IN, ZOOM_OUT
    }

    ;

    private Dimension size;//按钮大小
    private B_ICON icon_t;
    private ImageIcon icon;
    private boolean mouseOver = false;

    public IButtonUI(Dimension size_t, B_ICON icon_t) {
        this.size = size_t;
        if (icon_t != null) {
            this.icon_t = icon_t;
            setIcon();
        }
    }

    private void setIcon() {
        switch (icon_t) {
            case NEXT:
                icon = CreateIcon.createIcon("go-next.png");
                break;
            case PREVIOUS:
                icon = CreateIcon.createIcon("go-previous.png");
                break;
            case ROTATE_LEFT:
                icon = CreateIcon.createIcon("object-rotate-left.png");
                break;
            case ROTATE_RIGHT:
                icon = CreateIcon.createIcon("object-rotate-right.png");
                break;
            case FIT_BEST:
                icon = CreateIcon.createIcon("zoom-fit-best.png");
                break;
            case ZOOM_IN:
                icon = CreateIcon.createIcon("zoom-in.png");
                break;
            case ZOOM_OUT:
                icon = CreateIcon.createIcon("zoom-out.png");
                break;
        }
    }

    @Override
    public void installUI(JComponent c) {
        super.installUI(c);
        JButton b = (JButton) c;
        b.setPreferredSize(ObjectUtil.defaultIfNull(this.size, Constant.M_BUTTON_SIZE));
        b.setRolloverEnabled(true);
        b.setBorder(null);
        b.setFont(Constant.FONT_BASE_14);
    }

    @Override
    public void installListeners(final AbstractButton b) {
        super.installListeners(b);
        final JButton button = (JButton) b;
        b.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                mouseOver = true;
                button.repaint();
            }

            @Override
            public void mousePressed(MouseEvent e) {
                mouseOver = false;
                button.repaint();
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                mouseOver = true;
                button.repaint();
            }

            @Override
            public void mouseExited(MouseEvent e) {
                mouseOver = false;
                button.repaint();
            }
        });
    }

    @Override
    public void paint(Graphics _g, JComponent c) {
        super.paint(_g, c);
        Graphics2D g = (Graphics2D) _g;
        g.setStroke(new BasicStroke(1.5f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND));

        JButton button = (JButton) c;
        String text = button.getText();
        FontMetrics fm = SwingUtilities2.getFontMetrics(button, g);

        ButtonModel model = button.getModel();
       /* if (StrUtil.isNotBlank(text)) {
            int vWidth = fm.stringWidth(text);
            Dimension size = c.getPreferredSize();
            vWidth += 30;
            if (icon != null) {
                vWidth += icon.getIconWidth();
            }
            size.width = vWidth;
            c.setPreferredSize(size);
            c.setSize(size);

        }*/
        Dimension preferredSize = c.getPreferredSize();
        int width = preferredSize.width;
        int height = preferredSize.height;
        g.setColor(Constant.MAIN_BG_COLOR);
//        g.setColor(Color.RED);
        g.fillRect(0, 0, width, height);
/*		if ( b.isFocusPainted() && b.hasFocus() ) {
			g.setColor(Color.DARK_GRAY);
			g.fillRect(0, 0, c.getWidth(), c.getHeight());
		}*/

        if (mouseOver == true) {
            g.setColor(new Color(80, 80, 80));
            g.fillRoundRect(0, 0, width, height, 10, 10);
        }

        //perform UI specific press action, e.g. Windows L&F shifts text
        if (model.isArmed() && model.isPressed()) {
            g.setColor(Color.DARK_GRAY);
            g.fillRoundRect(0, 0, width, height, 10, 10);
        }

        g.setColor(new Color(93, 94, 94));
        g.drawRoundRect(0, 0, width, height, 10, 10);
        if (text != null && !text.equals("")) {
            int vWidth = fm.stringWidth(text);
            g.setColor(Constant.MAIN_FR_COLOR);
            int x = (width) / 2 - (vWidth / 2);
            if (size == Constant.B_BUTTON_SIZE)
                x += 15;
//            g.translate(c.getWidth()/2,height/2);
            g.drawString(text, x, height / 2 + 4);
        }

        //paint the icon
        if (icon != null) {
            int y = (height - icon.getIconHeight()) / 2;
            int x = (width - icon.getIconWidth()) / 2;
            if (icon_t == B_ICON.NEXT || icon_t == B_ICON.PREVIOUS)
                x = 5;
            g.drawImage(icon.getImage(), x, y, null);
        }
    }

}
